# -*- coding: utf-8 -*-
# Author   : ZhangQing
# Time     : 2025-08-12 13:00
# File     : main.py
# Project  : codebuddy_craft
# Desc     : 应用入口

"""
应用入口 🚀

启动FastAPI应用服务器
"""

import os
import uvicorn
import logging
from pathlib import Path

from backend.core.app_config import create_app
from backend.core.settings import settings
from backend.utils.logger import setup_logging, get_system_logger

# 设置日志
setup_logging(settings)
logger = logging.getLogger(__name__)
system_logger = get_system_logger()

# 创建应用
app = create_app()


def main():
    """主函数 🚀"""
    logger.info(f"🚀 启动 {settings.PROJECT_NAME} v{settings.PROJECT_VERSION}")
    logger.info(f"⚙️ 环境: {settings.ENVIRONMENT}")
    logger.info(f"🔧 调试模式: {'开启' if settings.DEBUG else '关闭'}")
    
    # 确保必要的目录存在
    os.makedirs(settings.DATA_DIR, exist_ok=True)
    os.makedirs(settings.LOGS_DIR, exist_ok=True)
    os.makedirs(settings.TEMP_DIR, exist_ok=True)
    
    # 启动服务器
    uvicorn.run(
        "main:app",
        host=settings.HOST,
        port=settings.PORT,
        reload=settings.RELOAD and settings.is_development,
        workers=settings.WORKERS,
        log_level=settings.LOG_LEVEL.lower(),
        access_log=True
    )


if __name__ == "__main__":
    main()